package com.zxy.ziems.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.ziems.server.domain.FieldValue;
import com.zxy.ziems.server.dto.FieldValueDto;
import com.zxy.ziems.server.mapper.FieldValueMapper;
import com.zxy.ziems.server.service.FieldValueService;
import com.zxy.ziems.server.vo.FieldValueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName FieldValueServiceImpl
 * @Description
 * @Author 邓伟
 * @Date 2023/11/29 17:08
 * @Version 1.0
 */
@Service
public class FieldValueServiceImpl extends ServiceImpl<FieldValueMapper, FieldValue> implements FieldValueService {

    @Autowired
    private FieldValueMapper fieldValueMapper;

    @Override
    public void batchInsert(String channelId,String dataId, String dataCode, List<FieldValueDto> fieldValueDtoList) {
        QueryWrapper<FieldValue> query = new QueryWrapper<>();
        query.lambda().eq(FieldValue::getDataId,dataId);
        fieldValueMapper.delete(query);
        List<FieldValue> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(fieldValueDtoList)) {
            for (FieldValueDto fieldValueDto : fieldValueDtoList) {
                FieldValue fieldValue = new FieldValue();
                fieldValue.setDataId(dataId);
                fieldValue.setDataCode(dataCode);
                fieldValue.setFieldValue(fieldValueDto.getFieldValue());
                fieldValue.setFieldCode(fieldValueDto.getFieldCode());
                fieldValue.setChannelId(channelId);
                list.add(fieldValue);
            }
        }
        saveBatch(list);
    }

    @Override
    public List<FieldValueVo> selectList(List<String> equipIdList) {
        return fieldValueMapper.selectField(equipIdList);
    }
}
